/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.console.action;

import java.util.Date;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.model.domain.system.School;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.cud.SchoolCUDService;
import com.us.usws.service.ro.SchoolROService;
import com.us.usws.support.uitls.JSONDataUtil;
import com.us.usws.support.uitls.Order;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

/**
 * Struts2 action
 * @author robot
 */
public class SchoolAction extends BasePageSearchAction<School> {

	private static final long serialVersionUID = 1L;

	private School school;

	@Inject
	private SchoolROService schoolROService;

	@Inject
	private SchoolCUDService schoolCUDService;

	private String uids;
	
	private VideoFile videoFile;

	/**
	 * ajax load the list of School
	 */
	public void ajaxLoadSchools() {
	    createDataPageUtil();
	    dataPageUtil.setOrder(Order.desc("create_date"));
		schoolROService.loadSchoolsByPage(dataPageUtil);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));
	}

	/**
	 * ajax update School
	 */
	public void ajaxUpdateSchool() {
		if (school != null) {
			String uid = school.getUid();
			String path = getRealPath();
			Date now = new Date(System.currentTimeMillis());
			WebContext wc = new SiteWebContext(getRequest(), getResponse());
			DOUser user = wc.getUser();
			
			if (StringUtils.isNotBlank(uid)) {
				school.setLastUpdByUid(user.getUid());
				school.setLastUpdDate(now);
				videoFile.setLastUpdByUid(user.getUid());
				videoFile.setLastUpdDate(now);
				videoFile.setUid(school.getVideoFileUid());
				
				String snapshot = videoFile.getSnapshot();
				if(StringUtils.isNotBlank(snapshot) && snapshot.contains("/")) {
					videoFile.setSnapshot(path + snapshot);
				}
				
				String videoPath = videoFile.getVideoPath();
				if(StringUtils.isNotBlank(videoPath) && videoPath.contains("/")) {
					videoFile.setVideoPath(path + videoPath);
				}
				
				schoolCUDService.updateSchool(school, videoFile);
			} else {
				school.setCreateByUid(user.getUid());
				school.setCreateDate(now);
				
				videoFile.setCreateByUid(user.getUid());
				videoFile.setCreateDate(now);
				String videoPath = videoFile.getVideoPath();
				String snapshot = videoFile.getSnapshot();
				if(StringUtils.isNotBlank(snapshot)) {
					videoFile.setSnapshot(path + snapshot);
				}
				
				if(StringUtils.isNotBlank(videoPath)) {
					videoFile.setVideoPath(path + videoPath);
				}
				schoolCUDService.addSchool(school, videoFile);
			}
			getOut().print(true);
		} else {
			getOut().print(false);
		}
	}

	/**
	 * ajax delete user.
	 */
	public void ajaxDeleteSchool() {
		schoolCUDService.deleteSchoolMore(uids);
	}
	
	public void ajaxLoadSchoolEduStage() {
		getOut().println(JSONDataUtil.jsonStringfromObject(schoolROService.loadSchoolEudStage(school.getUid())));
	}

	public void setSchool(School school) {
		this.school = school;
	}

	public School getSchool() {
		return school;
	}

	public String getUids() {
		return uids;
	}

	public void setUids(String uids) {
		this.uids = uids;
	}

	public VideoFile getVideoFile() {
		return videoFile;
	}

	public void setVideoFile(VideoFile videoFile) {
		this.videoFile = videoFile;
	}
}